#include "FilterSBox.h"
#include <stdio.h>

FilterSBox::FilterSBox(double support) : Filter(support)
{
	max_x_coord = 0.5 + (support - 1.0);
	max_y_coord = 0.5 + (support - 1.0);
	min_x_coord = -0.5 - (support - 1.0);
	min_y_coord = -0.5 - (support - 1.0);
}

FilterSBox::~FilterSBox()
{}

double FilterSBox::getWeight(double x, double y, int num_samples) const
{
	if(x < min_x_coord || y < min_y_coord)
		return 0;
	if(x > max_x_coord || y > max_y_coord)
		return 0;
	return 1.0 / (double)num_samples;
}

void FilterSBox::getWeights(double* weights, int samples, double* xs, double* ys) const
{
	//count the number of samples that are actually valid
	int num_valid_samples = 0;
	for(int i = 0; i < samples; i++) {
		//printf("Current x and y coord: %f\t%f\n", xs[i], ys[i]);
		//printf("Extremes: \t%f\t%f\t%f\t%f\n", max_x_coord, max_y_coord, min_x_coord, min_y_coord);
		if(xs[i] > min_x_coord && xs[i] < max_x_coord && ys[i] > min_y_coord && ys[i] < max_y_coord)
			num_valid_samples++;
	}
	//printf("number of valid samples: %d\n", num_valid_samples);
	double scaling_factor = 1.0 / (double)num_valid_samples;
	for(int i = 0; i < num_valid_samples; i++) {
		weights[i] = scaling_factor;
	}
}

